package orbito.entity;

import java.awt.Color;
import java.util.Random;

import orbito.physics.PhysicsSettings;
import orbito.util.Vec2;

public class PlanetoidFactory {
	
	/**
	 * Generate a planetoid with a calculated velocity that will put it in a gravitational 
	 * orbit around its parent planetoid given the parameters.
	 * 
	 * @param planetoidName      The name of the planetoid.
	 * @param planetoidMass      The mass of the planetoid.
	 * @param planetoidRadius    The radius of the planetoid.
	 * @param planetoidFillColor The inner fill color of the planetoid.
	 * @param parent             The parent of the planetoid that it will orbit.
	 * @param parentDist         The desired distance from its parent.
	 * @param retroOrbit         true if the planetoid's orbit should be retrograd around its parent, false otherwise.
	 * @param rand               The randomizer used for randomly selecting a position around its parent.
	 * @return                   The generated orbiting planetoid.
	 */
	public static Planetoid generateOrbitingPlanetoid(
			String planetoidName, double planetoidMass, double planetoidRadius, Color planetoidFillColor,
			Planetoid parent, double parentDist, boolean retroOrbit, Random rand) {
		// Randomly select its orientation around its parent.
		double orientation = rand.nextDouble() * Math.PI * 2;

		// From the above, calculate its position relative to its parent.
		Vec2 pos = new Vec2(parentDist, 0).rotate(orientation);

		// Calculate the speed to maintain perfect orbit around its parent.
		double speed = Math.sqrt(PhysicsSettings.gravitationalCoefficient * parent.getMass() / parentDist);
		
		// Calculate its velocity relative to its parent.
		Vec2 vel = pos.normal().unit().mul(speed);
		vel = retroOrbit ? vel : vel.negate();

		return new Planetoid(planetoidName, pos, vel, planetoidMass, planetoidRadius, planetoidFillColor, null);
	}
}
